package com.xwb.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xwb.dao.MemberDao;
import com.xwb.dao.TutorDao;
import com.xwb.domain.Member;
import com.xwb.domain.MemberWithTutor;
import com.xwb.domain.Tutor;
import com.xwb.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/members")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private TutorDao tutorDao;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 该方法用于保存队员信息
     */
    @PostMapping("/saveMember")
    public Result saveMember(@RequestBody Member member) {
        boolean flag = memberService.saveMember(member);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /**
     * 该方法用于保存队长和导师的数据,把数据分别存到member表里和tutor表里
     */
    @PostMapping("/saveLeaderAndTutor")
    public String saveLeaderAndTutor(@RequestBody String jsonData) {
        try {
            JsonNode rootNode = objectMapper.readTree(jsonData);
            JsonNode userNode = rootNode.get("member");
            JsonNode tutorNode = rootNode.get("tutor");

            Member member = objectMapper.readValue(userNode.toString(), Member.class);
            Tutor tutor = objectMapper.readValue(tutorNode.toString(), Tutor.class);

            memberDao.saveInfo(member);
            tutorDao.saveTutorInfo(tutor);

            return "Data saved successfully.";
        } catch (IOException e) {
            e.printStackTrace();
            return "Error while processing data.";
        }
    }

//    /**
//     * 该方法根据teamNumber来查询出同队的队员
//
//     */
//    @GetMapping("/{teamNumber}")
//    public Result selectTheSameTeamInfoByTeamNumber(@PathVariable String teamNumber) {
//        List members = memberService.selectTheSameTeamInfo(teamNumber);
//
//        Integer code = members != null ? Code.GET_OK : Code.GET_ERR;
//        String msg = members != null ? "查询同队信息成功！" : "查询同队信息失败！可能队伍号不存在，请重试";
//        return new Result(code,members,msg);
//    }
/*===========================================================================================*/

    /**
     * 该方法用于保存队员信息
     */
    @PostMapping("/saveMemberInTeam")
    public Result saveMemberInTeam(@RequestBody Member member){
        boolean flag = memberService.saveMemberInTeam(member);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /**
     * 该方法用于保存队长和导师的信息
     */
    @PostMapping("/saveMemberAndTutorInTeam")
    public Result saveMemberAndTutorInTeam(@RequestBody MemberWithTutor memberWithTutor){
        boolean flag = memberService.saveMemberAndTutorInTeam(memberWithTutor);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /**
     * 该方法用户查询team表中队伍号相同的同队成员
     */
    @GetMapping("/{teamNumber}")
    public Result selectByTeamNumber(@PathVariable String teamNumber){
        List<MemberWithTutor> memberWithTutorList = memberService.selectByTeamNumber(teamNumber);

        Integer code = memberWithTutorList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = memberWithTutorList != null ? "查询同队信息成功！" : "查询同队信息失败！可能队伍号不存在，请重试";
        return new Result(code,memberWithTutorList,msg);
    }

    /**
     * 该方法根据serialNumber删除掉对应学生在team表中的信息
     */
    @DeleteMapping("/{serialNumber}")
    public Result deleteBySerialNumber(@PathVariable Integer serialNumber){
        boolean flag = memberService.deleteBySerialNumber(serialNumber);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }


}


